package com.web.wps.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.web.wps.base.WpsResponse;
import com.web.wps.dto.WpsViewUrl;
import com.web.wps.service.WpsWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author jfan
 */
@RestController
@Slf4j
public class WpsWebController {
    @Resource
    private WpsWebService wpsWebService;

    @GetMapping(value = "/v1/api/file/getViewUrlWebPath")
    public ResponseEntity<Object> getViewUrlWebPath(String fileUrl) {
        log.info("fileUrl:{}", fileUrl);
        WpsViewUrl viewUrl = wpsWebService.getViewUrl(fileUrl, true);
        return WpsResponse.success(viewUrl);
    }
    /**
     * 回调方法，得到文件信息

     * @return
     */
    @GetMapping(value = "/v1/3rd/file/info")
    public ResponseEntity<Object> getFileInfo(String _w_filepath) {
        log.info("获取文件元数据,path:{},",_w_filepath);
        try {
            Map<String, Object> map = wpsWebService.getWebFileInfo(_w_filepath );
            return WpsResponse.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return WpsResponse.bad("获取文件元数据异常");
        }
    }
    /**
     * 回调通知
     */
    @PostMapping("/v2/3rd/onnotify")
    public ResponseEntity<Object> onNotify(@RequestBody JSONObject obj) {
        log.info("回调通知param:{}", JSON.toJSONString(obj));
        // TODO
        return WpsResponse.success();
    }

}